package com.aisile.admin.service.imol;

import com.aisile.admin.mapper.AdChannelMapper;
import com.aisile.admin.service.AdChannelService;
import com.aisile.common.exception.CustomExceptionThrows;
import com.aisile.model.admin.dtos.ChannelDto;
import com.aisile.model.admin.pojos.AdChannel;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    //分页查询
    @Override
    public ResponseResult findByPage(ChannelDto dto) {
        //先判断dto是否为空或错误(只判断错误,  不判断对)
        if(dto == null){
            CustomExceptionThrows.catchAdmin(AdminHttpCodeEnum.PARAM_INVALID);
        }
        //分页初始化
        dto.checkParam();
        //条件模糊 创建条件
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //拼接条件 根据name模糊查询
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(dto.getName()),AdChannel::getName,dto.getName());
        //执行分页
        IPage<AdChannel> iPage = this.page(new Page<>(dto.getPage(), dto.getSize()),lambdaQueryWrapper);

        //将ipage装到请求返回值里
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), Integer.valueOf(iPage.getTotal()+""));
        result.setData(iPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult saveAdchannel(AdChannel adChannel) {
        //先判断
        if(adChannel == null || adChannel.getName()==null || StringUtils.isEmpty(adChannel.getName())){
            return ResponseResult.errorResult(AdminHttpCodeEnum.DATA_EXIST);
        }
        //先查后插 根据name查询
        int i = this.count(
                Wrappers.<AdChannel>lambdaQuery().eq(
                        AdChannel::getName,adChannel.getName()
                )
        );
        //判断是否存在
        if (i>0){
            //大于0 存在返回报错信息
            return ResponseResult.errorResult(AdminHttpCodeEnum.DATA_EXIST);
        }
        //证明不存在 可以添加
        //当前时间
        adChannel.setCreatedTime(new Date());
        //添加
        this.save(adChannel);
        return ResponseResult.okResult(AdminHttpCodeEnum.SUCCESS);
    }

    //修改
    @Override
    public ResponseResult updateAdchannel(AdChannel adChannel) {
        //判断是否为空
        if(adChannel==null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        //当前时间
        adChannel.setCreatedTime(new Date());
        this.updateById(adChannel);
        return ResponseResult.errorResult(AdminHttpCodeEnum.SUCCESS);
    }

    //批量删除
    @Override
    public ResponseResult delsAdchannel(List<Integer> ids) {
        //先判断
        if (ids==null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        //循环遍历数组元素
        this.removeByIds(ids);
        return ResponseResult.errorResult(AdminHttpCodeEnum.SUCCESS);
    }


}
